<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserService
 *
 * @author Administrator
 */
class UserService {

    function findUserByLogin($user) {
        $userTable = D('user');
        $where['email'] = array('eq', $user['email']);
        $where['password'] = array('eq', $user['password']);
        $where['enabled'] = array('eq', $user['enabled']);

        return $userTable->where($where)->find();
    }

    //微博登陆
    function findUserByWeibo($user) {
        $where['weibo'] = array('eq', $user['weibo']);
        $where['weibo_type'] = array('eq', $user['weibo_type']);
        $where['enabled'] = array('eq', $user['enabled']);

        return D('user')->where($where)->find();
    }

    //微博注册
    function insertWeibo($user) {
        return D('user')->add($user);
    }

    function findUserByEmail($email) {
        $where['email'] = array('eq', $email);
        return D('user')->where($where)->find();
    }

    function findUserByEmailPassword($user) {
        $userTable = D('user');
        $where['email'] = array('eq', $user['email']);
        $where['password'] = array('eq', $user['password']);

        return $userTable->where($where)->find();
    }

    function findUserById($id) {
        $where['id'] = array('eq', $id);
        return D('user')->where($where)->find();
    }

    function insert($user) {
        $user['avatar'] = "../Public/images/baipaopao.jpg";
        $user['datetime'] = ConstantUtil::date();
        return D('user')->add($user);
    }

    function create() {
        return D('user')->create();
    }

    /**
     * 是否有效用户
     * @param type $user 
     */
    function validUser($user) {
        $userTable = D('user');
        $where['email'] = array('eq', $user['email']);
        $where['password'] = array('eq', md5($user['password']));
        $where['enabled'] = array('eq', 1);

        return $userTable->where($where)->find();
    }

    function update($user) {
        return D('user')->save($user);
    }

    function listRandomUser($random = '2') {
        return D('user')->query('SELECT nick_name FROM user ORDER BY rand() LIMIT ' . $random);
    }

}

?>
